<template>
	<view class="content">
		<uv-navbar title="" :placeholder="true" @leftClick="leftClick"></uv-navbar>
		<view class="content_box">
			<image class="logo" mode="widthFix" src="/static/image/logozfcy.png"></image>
			<view class="tel" @click="link('/user/login/tel')">手机号登录</view>
			<button class="wx" open-type="getPhoneNumber" @getphonenumber="tellogin">微信登录</button>
			<view class="zhuce" @click="link('/user/login/register')">注册新账号</view>
		</view>
	</view>
</template>

<script setup>
	import {link} from '@/utils/utils.js'
	import {
		ref
	} from 'vue'
	import {
		homeApi
	} from "@/api/index"
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	onLoad((open)=>{
		if(open.url){
			url.value = open.url
		}
	})
	const url = ref('')
	
	// 登录
	const codeTel = ref('')
	const unionid = ref('')
	const tellogin=(e)=>{
		codeTel.value = e.detail.code
		uni.login({
			provider: 'weixin', //使用微信登录
			success: function(loginRes) {
				wxlogin(loginRes.code);
			}
		});
	}
	const wxlogin = async (code,) => {
		let data = {
			do: 'wxcodelogin',
			code: code
		}
		let res = await homeApi.publicGet(data)
		if (res.error_no == '0') {
			uni.setStorageSync('token', res.token)
			uni.showToast({
				title: '登录成功！',
				icon: 'success',
				duration: 2000
			});
			setTimeout(() => {
				leftClick()
			}, 2000)
		} else {
			if(res.unionid){
				unionid.value = res.unionid
				setTel()
			}else{
				uni.showToast({
					title: '系统错误请联系管理员！',
					icon: 'none',
					duration: 2000
				});
			}
		}
	}
	const setTel = async()=>{
		let data = {
			code:codeTel.value,
			unionid:unionid.value
		}
		let res = await newsApi.postInfo(data,'userwxPhoneNumber')
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == 'userwxPhoneNumber') {
				if(res.columns[i].statuscode=='200'){
					uni.setStorageSync('token', res.columns[i].token)
					uni.showToast({
						title: '登录成功！',
						icon: 'success',
						duration: 2000
					});
					setTimeout(() => {
						leftClick()
					}, 2000)
				}
			}
		}
	}
	const leftClick = ()=>{
		if(url.value!=''){
			uni.redirectTo({
				url:decodeURIComponent(url.value)
			})
		}else{
			uni.navigateBack({
				delta: 1
			})
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
		&_box{
			width: 100%;
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			.logo{
				width: 55%;
				margin-top: 18vh;
			}
			.tel{
				border-radius: 30px;
				border: 1px solid #e94f06;
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #e94f06;
				font-size: 36rpx;
				margin-top: 150rpx;
			}
			.wx{
				width: 90%;
				height: 100rpx;
				background: #e94f06;
				border-radius: 30px;
				color: #fff;
				margin-top: 50rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			.zhuce{
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-top: 50rpx;
			}
		}
	}
</style>